package mop.gui.task;

import java.util.List;
import mop.controller.CreateTaskController;
import mop.gui.GUI;
import mop.model.task.tasktype.TaskTypeInterface;

/**
 * Dialog for selecting a task type.
 * 
 * @author  MOP Team 7
 */
public class SelectTaskTypeDialog extends javax.swing.JDialog {

    /**
     * Constructor.
     *
     * @param   gui
     * @param   createTaskController
     */
    public SelectTaskTypeDialog(GUI gui, CreateTaskController createTaskController)
    {
        super(gui.getMainWindow(), true);
        initComponents();
        setGui(gui);

        // Set controller
        this.createTaskController = createTaskController;

        // Load task types
        this.taskTypes = createTaskController.getTaskTypes();
        for (TaskTypeInterface t : this.taskTypes)
            this.cmbTaskType.addItem(t.getDescription());
    }

    /***************************************************************************
     *                          GUI reference
     ***************************************************************************/

    private GUI gui;

    /**
     * Sets the GUI.
     *
     * @param   gui
     * @post    gui is set.
     */
    private void setGui(GUI gui)
    {
        this.gui = gui;
    }

    /**
     * Returns the GUI reference.
     *
     * @return  this.gui
     */
    public GUI getGui()
    {
        return this.gui;
    }

    /***************************************************************************
     *                          Task Types
     ***************************************************************************/

    private List<TaskTypeInterface> taskTypes;

    /***************************************************************************
     *                          Controllers
     ***************************************************************************/

    private CreateTaskController createTaskController;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblType = new javax.swing.JLabel();
        cmbTaskType = new javax.swing.JComboBox();
        btnContinue = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select a Task Type");

        lblType.setText("Please select a task type for your new task...");

        btnContinue.setText("Continue");
        btnContinue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnContinueActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmbTaskType, 0, 236, Short.MAX_VALUE)
                    .addComponent(lblType)
                    .addComponent(btnContinue, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblType)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbTaskType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnContinue)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnContinueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnContinueActionPerformed
        try {
            TaskTypeInterface type = this.taskTypes.get(this.cmbTaskType.getSelectedIndex());
            if (createTaskController.canCreateTaskType(type.getId())) {
                (new CreateTaskDialog(gui, createTaskController, type)).setVisible(true);
                this.dispose();
            }
        } catch (Throwable exception) {
            gui.handleError(exception);
        }
    }//GEN-LAST:event_btnContinueActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnContinue;
    private javax.swing.JComboBox cmbTaskType;
    private javax.swing.JLabel lblType;
    // End of variables declaration//GEN-END:variables

}